<?php
class Kernel {
	public  $instance;
	public  $mmodule = '';
	public  $maction;
	public  $mvalues;
	public  $url;
	public function run() {
		Kernel::auto_load ();
	}
	public static function auto_load() {
		function __autoload($name) {
			$filename = 'class.' . strtolower ( $name ) . ".php";
			if (file_exists ( LIBS . $filename )) {
				require_once (LIBS . $filename);
			} elseif (file_exists ( PUBLICS . "controllers/" . $filename )) {
				require_once (PUBLICS . "controllers/" . $filename);
			}
		}
	}
	
	public function instance() {
		$url = new Url ( );
		$this->mmodule = $url->getModule () ? $url->getModule () : MODULE_DEFAULT;
		$this->maction = $url->getAction () ? $url->getAction () : ACTION_DEFAULT;
		$this->mvalues = $url->getValues ();
	}
	public function module() {
		return new $this->mmodule();
	}
	public function action() {
		return $this->maction;
	}
	public function values() {
		return $this->mvalues;
	}
	/**
	 * 查找文件
	 * @param $type
	 * @param $filename
	 * @param $require 是否强行获取
	 * @param $ext 文件名
	 */
	public static function find_file($type, $filename, $require = false, $ext = '') {
		if (strrpos ( $filename, '.' ) > 0) {
		
		} else if ($ext == '') {
			$ext = EXT;
		} else {
			$ext = '.' . $ext;
		}
		$find_key = $type . '/' . $filename . $ext;
		if ($type == 'config' || $type == 'i18n') {
			foreach ( self::$index_paths as $index ) {
				if (is_file ( $index . $find_key )) {
					$file [] = $index . $find_key;
				}
			}
		} else {
			foreach ( self::$index_paths as $index ) {
				if (is_file ( $index . $find_key )) {
					$file = $index . $find_key;
				}
			}
		}
		if (isset ( $file )) {
			return $file;
		} else {
			if ($require)
				throw new My_Exception ( 'core.cannot_find_file', $find_key );
			return false;
		}
	
	}
}
?>